import { createRouter, createWebHashHistory } from 'vue-router'
import { useUserStore } from '@/pinia/modules/user'
const routes = [{
  path: '/',
  redirect: '/login'
},
{
  path: '/login',
  name: 'Login',
  component: () => import('@/view/login/index.vue')
},
{
  path: '/demo',
  name: 'demo',
  component: () => import('@/view/demo/index.vue')
},
{
  path:'/home',
  component:()=>import('@/view/home/index.vue')
},
{
  path: '/:catchAll(.*)',
  hidden: true,
  component: () => import('@/view/error/index.vue')
}
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

const goBack = val =>{
  window.history.forward(1);
  // window.history.pushState(null,null,location.href);
  // window.history.pushState(null,null, document.URL);
}

router.beforeEach((to, from, next) => {
  //判断是不是点击的后退按钮 ,当点击后退按钮的时候当前url和to.fullPath 一样
  if (window.history && window.history.pushState) {
    window.addEventListener("popstate", goBack); // 回退时执行goback方法
  }
  if(to.name === "Login" && to.query.token){
    const userStore = useUserStore()
    userStore.ssoLoginIn({token: to.query.token,type:to.query.type,bizId:to.query.bizId})
      .catch(()=>{
        next()
      })
  } else {
    next()
  }
})

export default router
